<?php
$teacherAssign = $this->lastResult;
$idCycle = getParam("CYCLE");

$value = getParam("ID");
$pieces = explode(" - ", $value);
$id = $pieces[0];

$getTeacher = $this->db->queryToArray(sprintf("SELECT CONCAT(FIRST_NAME,' ',LAST_NAME) AS NAME FROM cllg_teacher WHERE ID='%s'",strtoupper($id)));
$who = " del catedrático ". $getTeacher[0]['NAME'];
$getCourse = $this->db->queryToArray(sprintf("SELECT NAME FROM cllg_courses_catalog WHERE CODE=%s", $teacherAssign[0]['FK_COURSE']));
$what = " para el curso ". $getCourse[0]['NAME'];

$dias = "";
if($teacherAssign[0]['LUNES'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="LUNES";
}
if($teacherAssign[0]['MARTES'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="MARTES";
}
if($teacherAssign[0]['MIERCOLES'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="MIERCOLES";
}
if($teacherAssign[0]['JUEVES'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="JUEVES";
}
if($teacherAssign[0]['VIERNES'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="VIERNES";
}
if($teacherAssign[0]['SABADO'] == 1){
    $dias = isEmpty($dias) ? $dias : "$dias, ";
    $dias.="SABADO";
}

$teacherAssign = $teacherAssign[0];
$section = $teacherAssign['SECTION'];

$sqlStudentAssign = "SELECT a.FK_STUDENT ID,
                            b.SCORE,
                            b.MIN_SCORE,
                            b.APROVED 
                     FROM   cllg_student_assign a,
                            cllg_assign_detail b
                     WHERE  a.STATUS = 1 
                     AND    a.ID = b.FK_ASSIGN 
                     AND    a.FK_CYCLE = " . sqlValue($idCycle, "int") . "
                     AND    b.FK_COURSE = " . sqlValue($teacherAssign["FK_COURSE"], "int") . "
                     AND    b.SECTION = " . sqlValue($teacherAssign["SECTION"], "text");

$sqlAll = "SELECT   a.ID, 
                    a.FIRST_NAME,
                    a.SECOND_NAME,
                    a.LAST_NAME, 
                    b.SCORE,
                    b.MIN_SCORE 
            FROM    cllg_student a,
                    (" . $sqlStudentAssign . ") b 
            WHERE   a.ID = b.ID
            ORDER   BY a.ID";
$r = $this->db->queryToArray($sqlAll);
$porcent = 100;
$showName = true;

?>
<div style="width:100%; overflow:auto;">
    <table width="<?= $porcent ?>%" border="0" cellspacing="0" cellpadding="0" class="table table-bordered table-hover table-striped">
        <tr>
                    <th colspan="14"><legend style="font-size: 13px">Estudiantes asignados<?php echo $who.$what." Seccion '".$section."' Días ".$dias; ?></legend></th>
        </tr>
        <tr>
            <th width="20%" scope="col">No. Carnet</th>
            <?php if ($showName) { ?>
                <th width="50%" align="left" scope="col">Estudiante</th>
            <?php } ?>
            <th width="30%" align="left" scope="col">Nota</th>
        </tr>
        <?php foreach ($r as $a) { ?>
            <tr>
                <td><?php echo $a["ID"]; ?></td>
                <?php if ($showName) { ?>
                    <td><?php echo $a["FIRST_NAME"] . " " . $a["SECOND_NAME"] . " " . $a["LAST_NAME"]; ?></td>
                <?php } ?>
                <?php
                $nota = $a["SCORE"];
                $strStyle = '';
                $notaminima = $a["MIN_SCORE"];
                if ($notaminima > $nota) {
                    $strStyle = "style='color:red'";
                }
                if ($nota == 0) {
                    $nota = "PENDIENTE";
                } else {
                    $nota = number_format($nota, 2);
                }
                ?>
                <td <?php print $strStyle; ?>><?php echo $nota; ?></td>
            </tr>
            <?php
        }
        ?>
    </table>
</div>